package com.leo.zerocopy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author du
 */
public class Server {
    public static void main(String[] args) throws IOException {

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress("127.0.0.1", 7000));


        ByteBuffer buffer = ByteBuffer.allocate(2048);
        while (true) {
            // 阻塞
            SocketChannel socketChannel = serverSocketChannel.accept();

            int read = 0;
            while ((read = socketChannel.read(buffer)) != -1) {
                //TODO 写入到其他FileChannel

                //倒带，和clear很类似，只是不改动limit；clear会将limit重置为capacity
                buffer.rewind();
            }
        }
    }
}
